<?php

namespace Johnm\Probackend;

use Illuminate\Support\ServiceProvider;
use Johnm\Probackend\Datatable\Datatable;
use Illuminate\Config\FileLoader as ConfigLoader;
use Illuminate\Config\Repository;
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;

class ProbackendServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    public function boot() {
        $this->package('johnm/probackend', 'probackend');
        include __DIR__ . '/../../helpers.php';
        include __DIR__ . '/../../filter.php';
        include __DIR__ . '/../../routes.php';
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        $this->registerServiceProvider();
        $this->registerDatatable();
        $this->registerFormer();
    }

    public function registerServiceProvider() {
        $app = $this->app;
        $app->register("Cartalyst\Sentry\SentryServiceProvider");
    }

    public function registerDatatable() {
        $this->app['datatable'] = $this->app->share(function($app) {
            return new Datatable;
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides() {
        return array('datatable', 'former');
    }

    public function registerFormer() {
        $app = $this->app;

        $app->bindIf('files', 'Illuminate\Filesystem\Filesystem');
        $app->bindIf('url', 'Illuminate\Routing\UrlGenerator');
        $app->bindIf('session.manager', function ($app) {
            return new SessionManager($app);
        });

        $app->bindIf('session', function ($app) {
            return $app['session.manager']->driver('array');
        }, true);

        $app->bindIf('request', function ($app) {
            $request = Request::createFromGlobals();
            if (method_exists($request, 'setSessionStore')) {
                $request->setSessionStore($app['session']);
            } else {
                $request->setSession($app['session']);
            }

            return $request;
        }, true);


        $app->bindIf('config', function ($app) {
            $fileloader = new ConfigLoader($app['files'], __DIR__ . '/../config');
            return new Repository($fileloader, 'config');
        }, true);


        $app->bindIf('translation.loader', function ($app) {
            return new FileLoader($app['files'], 'src/config');
        });

        $app->bindIf('translator', function ($app) {
            $loader = new FileLoader($app['files'], 'lang');
            return new Translator($loader, 'fr');
        });
        

        $app->bind('former.framework', function ($app) {
            return new  \Johnm\Probackend\Former\Framework\TwitterBootstrap3($app);
        });

        $app->singleton('former.populator', function ($app) {
            return new  \Johnm\Probackend\Former\Populator();
        });

        $app->singleton('former.dispatcher', function ($app) {
            return new  \Johnm\Probackend\Former\MethodDispatcher($app, \Johnm\Probackend\Former\Former::FIELDSPACE);
        });

        $app->singleton('former', function ($app) {
            return new  \Johnm\Probackend\Former\Former($app, $app->make('former.dispatcher'));
        });

        \Johnm\Probackend\Former\Helpers::setApp($app);
    }


  

}
